//
//  ZJUserChangeHeadPhoto.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/7.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJUserChangeHeadPhoto.h"

@interface ZJUserChangeHeadPhoto ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

@property (strong, nonatomic) NSArray * dataArr;
@property (strong, nonatomic) NSMutableArray * btnArr;
@property (strong, nonatomic) NSArray * photoArr;
@end

@implementation ZJUserChangeHeadPhoto

- (void)viewDidLoad {
    [super viewDidLoad];
   self.view.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.3];
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 8.0f;
    
    self.photoArr = @[@"wdzh_pic_txa",@"wdzh_pic_txj",@"wdzh_pic_txk",@"wdzh_pic_txl",@"wdzh_pic_txb",@"wdzh_pic_txc",@"wdzh_pic_txd",@"wdzh_pic_txe",@"wdzh_pic_txf",@"wdzh_pic_txg",@"wdzh_pic_txh",@"wdzh_pic_txi"];
    
    self.btnArr = [NSMutableArray new];
    NSDictionary *parameter = [LRNetwork requestParameter:@{}];
    NSString *url = [LRNetwork requestURL:@"cgi/avatar_list.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        self.dataArr = returnValue[@"avatar"];
        
        [self setupScrollview];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}


-(void)setupScrollview{
    
   float width = (self.bgView.width - 27 * 4)/3;

    for (int i = 0; i < _dataArr.count; i++) {
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake( 27 + i % 3 * (width + 27), 17 + i / 3 * (width + 17), width, width)];
        
        NSDictionary * dict = self.dataArr[i];

        [btn setBackgroundColor:[UIColor clearColor]];

        [btn setImage:[UIImage imageNamed:self.photoArr[i]] forState:0];
        btn.tag = i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:(UIControlEventTouchUpInside)];
        
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = width/2;

        [self.scrollview addSubview:btn];
        
        [_btnArr addObject:btn];
    }

}

-(void)clickBtn:(UIButton *)sender{
    
    NSDictionary * dict = self.dataArr[sender.tag];

    NSDictionary *parameter = [LRNetwork requestParameter:@{@"avatar_name":dict[@"avatar_name"]}];
    NSString *url = [LRNetwork requestURL:@"cgi/avatar.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
//        [HUD showSucceedMsg:@"修改成功!"];
        ZJUserModel * model = GET_USERINFO;
        model.avatar = dict[@"url"];
        SYN_USER(model);
        if (_delegate != nil) {
            [_delegate onClikModel:model];
        }
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
